/**
 * jims
 */
package com.jims.master.modules.med.entity;

import com.jims.master.common.config.OperType;
import com.jims.master.common.persistence.DataEntity;
import com.jims.master.common.utils.NumberUtils;
import com.jims.master.modules.doctor.entity.PatMasterIndex;
import org.hibernate.validator.constraints.Length;

/**
 * 一卡通办理Entity
 * @author 陈晓阳
 * @version 2017-05-22
 */
public class MedicalCardMaster extends DataEntity<MedicalCardMaster> {
	
	private static final long serialVersionUID = 1L;
	private String patientId;		// patient_id
	private String cardNo;		// card_no
	private String cardType;		// card_type
	private String cardStatus;		// 状态0-可用 1-挂起 2-注销';
	private String rcptNo;		// 充值收据号
	private String rechangeFlag;		// 是否已经充值
	private double deposit;		// 卡押金
	private double balance;		// 余额
	private String idCard;		// 省份正号码
	private String rechangeType;		// 充值方式 0 现金 1 微信 3 银行卡
//	private String orgId;		// 机构ID
	private String name;		// 姓名
	private String sex;		// 性别
	private String phone;		// 手机号
	private String backCard;		// 银行卡
	private String carteVital;		// 医保卡
	private String age;		// 年龄
	private String expand1;		// expand1
	private String expand2;		// expand2
	private String expand3;		// expand3
    private String startTime;
    private String endTime;//
    private String nation;//名族
    private String cardNewType;//建卡方式
    private String flag;// 1 新建 2 修改  3 充值  4 补办  5 退卡
    private String isRechange;//余额合并  0 合并 1 不合并
    private String oldId;//旧卡Id;
    private double oldBalance;//挂失卡余额
    private String nameQuery;//
    private String clinicNo;
    private MedicalCardRechangeRec medicalCardRechangeRec=new MedicalCardRechangeRec();
    private PatMasterIndex patMasterIndex=null;
    private double consumeMoney;

    public PatMasterIndex getPatMasterIndex() {
        return patMasterIndex;
    }

    public void setPatMasterIndex(PatMasterIndex patMasterIndex) {
        this.patMasterIndex = patMasterIndex;
    }

    public MedicalCardRechangeRec getMedicalCardRechangeRec() {
        return medicalCardRechangeRec;
    }

    public void setMedicalCardRechangeRec(MedicalCardRechangeRec medicalCardRechangeRec) {
        this.medicalCardRechangeRec = medicalCardRechangeRec;
    }

    public String getClinicNo() {
        return clinicNo;
    }

    public void setClinicNo(String clinicNo) {
         this.clinicNo = clinicNo;
    }

    public String getNameQuery() {
        return nameQuery;
    }

    public void setNameQuery(String nameQuery) {
        this.nameQuery = nameQuery;
    }

    public double getOldBalance() {
        return oldBalance;
    }

    public void setOldBalance(double oldBalance) {
        this.oldBalance = oldBalance;
    }

    public String getOldId() {
        return oldId;
    }

    public void setOldId(String oldId) {
        this.oldId = oldId;
    }

    public String getIsRechange() {
        return isRechange;
    }

    public void setIsRechange(String isRechange) {
        this.isRechange = isRechange;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    public String getCardNewType() {
        return cardNewType;
    }

    public void setCardNewType(String cardNewType) {
        this.cardNewType = cardNewType;
    }

    private String  homeplace;//出生地

    public String getNation() {
        return nation;
    }

    public void setNation(String nation) {
        this.nation = nation;
    }

    public String getHomeplace() {
        return homeplace;
    }

    public void setHomeplace(String homeplace) {
        this.homeplace = homeplace;
    }

    public MedicalCardMaster() {
		super();
	}

	public MedicalCardMaster(String cardNo){
		this.cardNo=cardNo;
	}

	@Length(min=1, max=64, message="patient_id长度必须介于 1 和 64 之间")
	public String getPatientId() {
		return patientId;
	}

	public void setPatientId(String patientId) {
		this.patientId = patientId;
	}
	
	@Length(min=1, max=64, message="card_no长度必须介于 1 和 64 之间")
	public String getCardNo() {
		return cardNo;
	}

	public void setCardNo(String cardNo) {
		this.cardNo = cardNo;
	}
	
	@Length(min=1, max=64, message="card_type长度必须介于 1 和 64 之间")
	public String getCardType() {
		return cardType;
	}

	public void setCardType(String cardType) {
		this.cardType = cardType;
	}
	
	@Length(min=1, max=11, message="状态0-可用 1-挂起 2-注销';长度必须介于 1 和 11 之间")
	public String getCardStatus() {
        if(cardStatus!=null&&!"".equals(cardStatus)&&cardStatus.contains(",")){
            cardStatus=cardStatus.substring(0,cardStatus.length()-1);
        }
		return cardStatus;
	}

	public void setCardStatus(String cardStatus) {
		this.cardStatus = cardStatus;
	}
	
	@Length(min=0, max=64, message="充值收据号长度必须介于 0 和 64 之间")
	public String getRcptNo() {
		return rcptNo;
	}

	public void setRcptNo(String rcptNo) {
		this.rcptNo = rcptNo;
	}
	
	@Length(min=1, max=11, message="是否已经充值长度必须介于 1 和 11 之间")
	public String getRechangeFlag() {
		return rechangeFlag;
	}

	public void setRechangeFlag(String rechangeFlag) {
		this.rechangeFlag = rechangeFlag;
	}
	
//	public String getDeposit() {
//        if(deposit==null || "".equals(deposit))
//		return deposit;
//	}
//
//	public void setDeposit(String deposit) {
//		this.deposit = deposit;
//	}
//
//	public String getBalance() {
//		return balance;
//	}
//
//	public void setBalance(String balance) {
//		this.balance = balance;
//	}
//

    public double getDeposit() {
        return deposit;
    }

    public void setDeposit(double deposit) {
        this.deposit = deposit;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    @Length(min=1, max=64, message="省份正号码长度必须介于 1 和 64 之间")
	public String getIdCard() {
		return idCard;
	}

	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}
	
	@Length(min=1, max=11, message="充值方式 0 现金 1 微信 3 银行卡长度必须介于 1 和 11 之间")
	public String getRechangeType() {
		return rechangeType;
	}

	public void setRechangeType(String rechangeType) {
		this.rechangeType = rechangeType;
	}
	
//	@Length(min=1, max=64, message="机构ID长度必须介于 1 和 64 之间")
//	public String getOrgId() {
//		return orgId;
//	}
//
//	public void setOrgId(String orgId) {
//		this.orgId = orgId;
//	}
//
	@Length(min=1, max=64, message="姓名长度必须介于 1 和 64 之间")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Length(min=1, max=2, message="性别长度必须介于 1 和 2 之间")
	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
	
	@Length(min=0, max=64, message="手机号长度必须介于 0 和 64 之间")
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	@Length(min=0, max=64, message="银行卡长度必须介于 0 和 64 之间")
	public String getBackCard() {
		return backCard;
	}

	public void setBackCard(String backCard) {
		this.backCard = backCard;
	}
	
	@Length(min=0, max=64, message="医保卡长度必须介于 0 和 64 之间")
	public String getCarteVital() {
		return carteVital;
	}

	public void setCarteVital(String carteVital) {
		this.carteVital = carteVital;
	}
	
	@Length(min=1, max=11, message="年龄长度必须介于 1 和 11 之间")
	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}
	
	@Length(min=0, max=64, message="expand1长度必须介于 0 和 64 之间")
	public String getExpand1() {
		return expand1;
	}

	public void setExpand1(String expand1) {
		this.expand1 = expand1;
	}
	
	@Length(min=0, max=64, message="expand2长度必须介于 0 和 64 之间")
	public String getExpand2() {
		return expand2;
	}

	public void setExpand2(String expand2) {
		this.expand2 = expand2;
	}
	
	@Length(min=0, max=64, message="expand3长度必须介于 0 和 64 之间")
	public String getExpand3() {
		return expand3;
	}

	public void setExpand3(String expand3) {
		this.expand3 = expand3;
	}


    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }


    /**
     * 操作类型
     * @return
     */
    public OperType operType(){
        OperType returnVal=null;
        if(this.flag!=null&&!"".equals(flag)){
            switch (Integer.parseInt(flag)){
                case 1:
                    returnVal=OperType.新建;
                    break;
                case 2:
                    returnVal=OperType.修改;
                    break;
                case 3:
                    returnVal=OperType.充值;
                    break;
                case 4:
                    returnVal=OperType.补办;
                    break;
                case 5:
                    returnVal=OperType.退卡;
                    break;
                default:returnVal=null;
                    break;
            }
            return returnVal;
        }else{
            return returnVal;
        }
    }

    /**
     * 是否充值
     * @return
     */
    public boolean isRechange(){
        boolean returnVal=false;
        if(!"-1".equals(rechangeType)){
            medicalCardRechangeRec.setPatientId(getPatientId());
            medicalCardRechangeRec.setCardNo(getCardNo());
            medicalCardRechangeRec.setRcptNo(getRcptNo());
            medicalCardRechangeRec.setRechangeMoney(getDeposit() + "");
            medicalCardRechangeRec.setBalance(getDeposit() + "");
            medicalCardRechangeRec.setRechangeType(getRechangeType());
            returnVal=true;
        }
        return returnVal;
    }

    /**
     * 设置数据
     */
    public void setData(String patientId,PatMasterIndex patMasterIndex1){
        if(operType().equals(OperType.新建)&&isRechange()){
            setBalance(getDeposit());
            setRechangeFlag("0");
            setRcptNo(NumberUtils.getRcptNo());
            setPatientId(patientId);
        }else if(operType().equals(OperType.新建)&&!isRechange()){
            setBalance(0);
            setRechangeFlag("1");
            setPatientId(patientId);
        }else if(operType().equals(OperType.修改)){
            patMasterIndex1.setAge(getAge());
            patMasterIndex1.setPhone(getPhone());
            patMasterIndex1.setNation(getNation());
            patMasterIndex1.setHomeplace(getHomeplace());
            patMasterIndex1.setAddress(getHomeplace());
            patMasterIndex1.setSex(getSex());
            patMasterIndex1.setChargeType(getExpand1());
            patMasterIndex=patMasterIndex1;
        }else if(operType().equals(OperType.充值)){
            setBalance((balance + deposit));
            medicalCardRechangeRec.setCardNo(getCardNo());
            medicalCardRechangeRec.setPatientId(getPatientId());
            medicalCardRechangeRec.setRcptNo(NumberUtils.getRcptNo());
            medicalCardRechangeRec.setRechangeMoney(getDeposit() + "");
            medicalCardRechangeRec.setRechangeType(getRechangeType());
            medicalCardRechangeRec.setBalance(getBalance() + "");
        }else if(operType().equals(OperType.补办)){
             if(isAddMoney()){
                if(isRechange()){
                    setBalance(getDeposit()+oldBalance);
                    setRechangeFlag("0");
                    setRcptNo(NumberUtils.getRcptNo());
                    medicalCardRechangeRec.setPatientId(getPatientId());
                    medicalCardRechangeRec.setCardNo(getCardNo());
                    medicalCardRechangeRec.setRcptNo(getRcptNo());
                    medicalCardRechangeRec.setRechangeMoney(getDeposit()+"");
                    medicalCardRechangeRec.setRechangeType(getRechangeType());
                    medicalCardRechangeRec.setBalance(getBalance()+"");
                }else{
                    //不充值
                    setBalance(oldBalance);
                    setRechangeFlag("1");
                }
            }
        }else if(operType().equals(OperType.退卡)){
            medicalCardRechangeRec.setPatientId(getPatientId());
            medicalCardRechangeRec.setCardNo(getCardNo());
            medicalCardRechangeRec.setRcptNo(NumberUtils.getRcptNo());
            medicalCardRechangeRec.setRechangeMoney(-getBalance()+"");
            medicalCardRechangeRec.setRechangeType("-1");
        }
     }

    /**
     * 是否余额合并
     * @return
     */
    public boolean isAddMoney(){
        boolean returnVal=false;
        if(this.isRechange!=null&&this.getIsRechange().equals("0")&&getOldBalance()>0){
            returnVal=true;
        }
        return returnVal;
    }

    public double getConsumeMoney() {
        return consumeMoney;
    }

    public void setConsumeMoney(double consumeMoney) {
        this.consumeMoney = consumeMoney;
    }
}